package m202312.d04;

/**
 * @author haocongshun
 * @date 2023/12/04 18:19:46
 */

/**
 * 167. 两数之和 II - 输入有序数组
 */
public class Q167 {

    public static void main(String[] args) {
        int[] ints = new Q167().twoSum(new int[]{-10, -8, -2, 1, 2, 5, 6}, 0);
        System.out.println();
    }

    public int[] twoSum(int[] numbers, int target) {
        int start = 0;
        int end = numbers.length-1;
        while (start < end) {
            int sum = numbers[start] + numbers[end];
            if (sum == target) {
                return new int[]{start + 1, end + 1};
            } else if (sum > target) {
                end--;
            } else {
                start++;
            }
        }
        throw new RuntimeException("no result");
    }

    public int findEnd(int[] numbers, int target) {
        for (int i = 1; i < numbers.length; i++) {
            if (numbers[i] + numbers[0] >= target) {
                return i;
            }
        }
        return numbers.length - 1;
    }
//    public int[] twoSum(int[] numbers, int target) {
//        for (int i = 0; i < numbers.length - 1; i++) {
//            for (int j = i + 1; j < numbers.length; j++) {
//                int sum = numbers[i] + numbers[j];
//                if (sum == target) {
//                    return new int[]{i + 1, j + 1};
//                } else if (sum > target) {
//                    break;
//                }
//            }
//        }
//        throw new RuntimeException("no result");
//    }
}
